package com.xuwang.service.impl;

import com.xuwang.Util.DateTimeUtils;
import com.xuwang.Util.JWTutil;
import com.xuwang.Util.RandomNumberUtil;
import com.xuwang.mapper.AdditiveMapper;
import com.xuwang.mapper.InventoryMapper;
import com.xuwang.pojo.FoodRelation;
import com.xuwang.pojo.Sale;
import com.xuwang.mapper.SaleMapper;
import com.xuwang.pojo.SaleFood;
import com.xuwang.vo.InventorySelectVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聚餐记录表(Sale)表服务实现类
 *
 * @author abaaba
 * @since 2021-09-20 16:47:06
 * @version 1.0
 */
@Service("saleService")
public class SaleServiceImpl {
    @Resource
    private SaleMapper saleMapper;
    @Resource
    private AdditiveMapper additiveMapper;
    @Resource
    private InventoryMapper inventoryMapper;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.saleMapper.selectById(id));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param index 查询起始位置
     * @return 对象列表
     */
    public Map<String, Object> selectForPage(int m, int index, int page,Integer saleTypeId, String starTime,
                                             String endTime, String companyName, HttpServletRequest request) {
        // 获取当前表中的总记录
        if(m==2){
            String token = request.getHeader("Authorization");
            String userName = JWTutil.getUserName(token);

//            String userName="菜品人";
            companyName=additiveMapper.selectCompanyNameByUsername(userName);
        }
        // 获取当前表中的总记录
        int tableCount = this.saleMapper.selectForCount(saleTypeId,starTime,endTime,companyName);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / page + 1;
        // 计算每页开始的下标值
        index = (index - 1) * page;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.saleMapper.selectForPage(index, page,saleTypeId,starTime,endTime,companyName));
        return map;
    }

    /**
     * 新增数据
     *
     * @param sale 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insert(Sale sale,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userName = JWTutil.getUserName(token);
//        String userName="销售人";

        String tishi=null;
        Map<String, Object> map = new HashMap<>();
        int companyId = additiveMapper.selectCompanyIdByUsername(userName);
        String companyName = additiveMapper.selectCompanyNameByUsername(userName);
        sale.setCompanyId(companyId);
        sale.setCompanyName(additiveMapper.selectCompanyNameByUsername(userName));
        List<SaleFood> saleFoodList = sale.getSaleFoodList();
        for (SaleFood saleFood : saleFoodList) {

            String[] split = saleFood.getFoodBatchNo().split(",");
            saleFood.setFoodName(split[0]);
            saleFood.setFoodBatchNo(split[1]);
            Double aDouble = inventoryMapper.selectCountNumberByNameAndCompanyId(saleFood.getFoodName(), sale.getCompanyId());
            aDouble= aDouble==null?0:aDouble;
            if (saleFood.getNumber()
                    >aDouble) {
                tishi+="批号为"+saleFood.getFoodBatchNo()+"的"+saleFood.getFoodName()+"库存不足！  ";
            }
        }
        if (tishi!=null){
            map.put("code", 201);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
            map.put("msg", tishi);
        }else {
            sale.setCompanyName(companyName);
            sale.setCreateTime(Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
            this.saleMapper.insert(sale);
            for (SaleFood saleFood : saleFoodList) {
                inventoryMapper.updateNumberByNoAndCompanyId(
                        inventoryMapper.selectCountNumberByNameAndCompanyId(saleFood.getFoodName(),sale.getCompanyId() )-saleFood.getNumber(),
                        saleFood.getFoodBatchNo(),companyId);
                saleFood.setSaleId(sale.getId());
            }

            saleMapper.insertsaleFoods(saleFoodList);
            map.put("code", 200);   // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
            map.put("msg", "新增成功");
        }


        return map;
    }


}